package com.modern.js.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modern.js.entity.Customer;
import com.modern.js.entity.Employee;
import com.modern.js.service.ICustomerService;
import com.modern.js.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-03-28
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService iCustomerService;


    /**
     * 查询全部的客户信息
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public Map getList(Integer page, Integer limit,String empName,String createTime){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Customer> customerPage = new Page<Customer>();
        customerPage.setSize(limit);//设置每页记录数
        customerPage.setCurrent(page);//设置当前页码
        IPage<Customer> iPage = iCustomerService.selectList(customerPage,empName,createTime);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }


    @GetMapping("/byId")
    public Map selectById(Integer page, Integer limit,String telphone,String customerName,HttpSession session){
        Employee employee = (Employee) session.getAttribute("employee");
        Integer empId = employee.getEmpId();
        System.out.println("你好世界："+empId);
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Customer> customerPage = new Page<Customer>();
        customerPage.setSize(limit);//设置每页记录数
        customerPage.setCurrent(page);//设置当前页码
        IPage<Customer> iPage = iCustomerService.selectById(customerPage,empId,telphone,customerName);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }

    @DeleteMapping("/del/{customerId}")
    public boolean delCustomer(@PathVariable Integer customerId){
        Customer customer = new Customer();
        customer.setIsDel(0);
        customer.setCustomerId(customerId);
        boolean result = iCustomerService.updateById(customer);
        return result;
    }

    @PutMapping("/update")
    public boolean delCustomer(Customer customer){
        customer.setUpdateTime(StringUtils.getNowTime());
        boolean result = iCustomerService.updateById(customer);
        return result;
    }

    @PostMapping("/add")
    public boolean addCustomer(Customer customer,HttpSession session){
        customer.setUpdateTime(StringUtils.getNowTime());
        customer.setCreateTime(StringUtils.getNowTime());
        Employee employee =(Employee) session.getAttribute("employee");
        customer.setIsDel(1);
        customer.setEmpId(employee.getEmpId());
        boolean result = iCustomerService.save(customer);
        return result;
    }


    @GetMapping("/selectCustomer")
    public List selectCustomer(HttpSession session){
        Employee employee =(Employee) session.getAttribute("employee");
        Integer empId = employee.getEmpId();
        List<Customer> list = iCustomerService.selectCustomer(empId);
        for (Customer customer:list){
            System.out.println(customer);
        }
        return list;
    }

    @GetMapping("/selectIsOrders")
    public Map selectCustomerIsOrders(Integer page, Integer limit,HttpSession session){
        System.out.println("你好松哥");
        Employee employee = (Employee) session.getAttribute("employee");
        Integer empId = employee.getEmpId();
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Customer> customerPage = new Page<Customer>();
        customerPage.setSize(limit);//设置每页记录数
        customerPage.setCurrent(page);//设置当前页码
        IPage<Customer> iPage = iCustomerService.selectCustomerIsOrders(customerPage,empId);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }

}
